package com.situ.mall.function.lambda;

import com.situ.mall.function.pojo.Student;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.function.Consumer;

public class Demo {
    @Test
    public void test1() {
        //匿名内部类
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("run");
            }
        }).start();

        // 其实我们最关注的是run方法和里面要执行的代码，为了指定run的方法体，不得不需要Runnable接口的实现类；
        // 必须覆盖重写抽象run方法，所以方法名称、方法参数、方法返回值不得不再写一遍，且不能写错；
        // Lambda表达式体现的是函数式编程思想,只需要将要执行的代码放到函数中
        //   （参数列表）->{
        //    }
        new Thread(() -> {
            System.out.println("run");
        }).start();

        //Alt+Enter
        new Thread(() -> System.out.println("run")).start();
    }

    @Test
    public void test12() {
        ArrayList<Student> list = new ArrayList<>();
        list.add(new Student(1, "张三", 34));
        list.add(new Student(1, "李四", 45));
        list.add(new Student(1, "王五", 87));
        list.add(new Student(1, "赵六", 22));

        Collections.sort(list, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getAge() - o2.getAge();
            }
        });

        Collections.sort(list, (Student o1, Student o2) -> {
            return o1.getAge() - o2.getAge();
        });

        Collections.sort(list, (o1, o2) -> o1.getAge() - o2.getAge());

        for (Student student : list) {
            System.out.println(student);
        }
    }

    @Test
    public void test44() {
        buyGoods(500, new Consumer<Double>() {
            @Override
            public void accept(Double money) {
                System.out.println("我买了一个" + money + "快钱商品");
            }
        });

        buyGoods(500, money -> System.out.println("我买了一个" + money + "快钱商品"));
    }

    private void buyGoods(double money, Consumer<Double> consumer) {
        consumer.accept(money);
    }

}
